package me.kuye.jkuye.plugin.activerecord;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import me.kuye.jkuye.helper.DatabaseHelper;
import me.kuye.jkuye.plugin.IPlugin;
import me.kuye.jkuye.util.StringUtil;

public class ActiveRecordPlugin implements IPlugin {
	private int transactionLevel;
	private String configName = DatabaseHelper.MAIN_CONFIG_NAME;
	private Boolean devMode = null;
	private Config config;
	private DataSource dataSource;
	private IDataSourceProvider dataSourceProvider;
	private List<Table> tableList = new ArrayList<Table>();
	private boolean isInit;

	public ActiveRecordPlugin(Config config) {
		if (config == null) {
			throw new IllegalArgumentException("config can not be null");
		}
		this.config = config;
	}

	public ActiveRecordPlugin(DataSource dataSource) {
		this(DatabaseHelper.MAIN_CONFIG_NAME, dataSource);
	}

	public ActiveRecordPlugin(String configName, DataSource dataSource) {
		this(configName, dataSource, Connection.TRANSACTION_READ_COMMITTED);
	}

	public ActiveRecordPlugin(DataSource dataSource, int transactionLevel) {
		this(DatabaseHelper.MAIN_CONFIG_NAME, dataSource, transactionLevel);
	}

	public ActiveRecordPlugin(String configName, DataSource dataSource, int transactionLevel) {
		if (StringUtil.isEmpty(configName))
			throw new IllegalArgumentException("configName can not be empty");
		if (dataSource == null)
			throw new IllegalArgumentException("dataSource can not be null");
		this.configName = configName.trim();
		this.dataSource = dataSource;
		this.setTransactionLevel(transactionLevel);
	}

	public ActiveRecordPlugin(IDataSourceProvider dataSourceProvider) {
		this(DatabaseHelper.MAIN_CONFIG_NAME, dataSourceProvider);
	}

	public ActiveRecordPlugin(String configName, IDataSourceProvider dataSourceProvider) {
		this(configName, dataSourceProvider, Connection.TRANSACTION_READ_COMMITTED);
	}

	public ActiveRecordPlugin(IDataSourceProvider dataSourceProvider, int transactionLevel) {
		this(DatabaseHelper.MAIN_CONFIG_NAME, dataSourceProvider, transactionLevel);
	}

	public ActiveRecordPlugin(String configName, IDataSourceProvider dataSourceProvider, int transactionLevel) {
		if (StringUtil.isEmpty(configName))
			throw new IllegalArgumentException("configName can not be empty");
		if (dataSourceProvider == null)
			throw new IllegalArgumentException("dataSourceProvider can not be null");
		this.configName = configName.trim();
		this.dataSourceProvider = dataSourceProvider;
		this.setTransactionLevel(transactionLevel);
	}

	public ActiveRecordPlugin addMapping(String tableName, String primaryKey, Class<? extends Model<?>> modelClass) {
		tableList.add(new Table(tableName, primaryKey, modelClass));
		return this;
	}

	/**
	* @Description: 设置事务级别，只能为0 1 2 4 8 
	* @param transactionLevel  事务级别
	* @return ActiveRecordPlugin 
	* @throws
	*/ 
	private ActiveRecordPlugin setTransactionLevel(int transactionLevel) {
		int level = transactionLevel;
		if (level != 0 && level != 1 && level != 2 && level != 4 && level != 8) {
			throw new IllegalArgumentException("The transactionLevel only be 0, 1, 2, 4, 8");
		}
		this.transactionLevel = transactionLevel;
		return this;
	}

	@Override
	public boolean init() {
		if(isInit){
			return true;
		}
		if(dataSourceProvider!=null){
			dataSource=dataSourceProvider.getDataSource();
		}
		if(dataSource==null){
			throw new RuntimeException("the datasource can not be null");
		}
		config=new Config();
		return false;
	}

	@Override
	public boolean destory() {
		return false;
	}

	public Boolean getDevMode() {
		return devMode;
	}

	public void setDevMode(Boolean devMode) {
		this.devMode = devMode;
	}
}
